﻿#include "volumeset.h"
#include "dcuinformation.h"
#include "widget.h"

const QHostAddress PCUAddress = QHostAddress("230.10.10.11");
const int          PCUPort    = 20010;

VolumeSet::VolumeSet(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);
    setAutoFillBackground(true);
    background = palette();
    background.setBrush(QPalette::Window, QBrush(QPixmap(":/images/defaultUI.png")));
    setPalette(background);

    AutoVolumeLB = new QLabel(this);
    AutoVolumeLB->setText(tr("TTS音量"));
    AutoVolumeLB->setFont(QFont(ZITI, 20, QFont::Bold));
    AutoVolumeLB->setGeometry(QRect(250, 72 - 30, 120, 45));
    AutoVolumeLB->setStyleSheet("color:white;text-align:center");

    AutoVolumeDecrease = new QPushButton(this);
    AutoVolumeDecrease->setGeometry(QRect(370, 75 - 30, 40, 40));
    AutoVolumeDecrease->setStyleSheet(
        "QPushButton{border-image: url(:/images/DecreasePulled.png)}"
        "QPushButton:pressed{border-image: url(:/images/DecreasePushed.png);}");
    connect(AutoVolumeDecrease, SIGNAL(clicked()), this, SLOT(DecreaseAutoVolume()));
    AutoVolumeDecrease->hide();

    AutoVolumeIncrease = new QPushButton(this);
    AutoVolumeIncrease->setGeometry(QRect(470, 75 - 30, 40, 40));
    AutoVolumeIncrease->setStyleSheet(
        "QPushButton{border-image: url(:/images/IncreasePulled.png)}"
        "QPushButton:pressed{border-image: url(:/images/IncreasePushed.png);}");
    connect(AutoVolumeIncrease, SIGNAL(clicked()), this, SLOT(IncreaseAutoVolume()));
    AutoVolumeIncrease->hide();

    AutoVolume = new QLabel(this);
    AutoVolume->setText(QString("%1").arg(StationInfo.AutoVolume));
    AutoVolume->setAlignment(Qt::AlignCenter);
    AutoVolume->setFont(QFont(ZITI, 20, QFont::Normal));
    AutoVolume->setGeometry(QRect(420 - 10, 75 - 30, 60, 40));
    AutoVolume->setStyleSheet("color:black;text-align:center;background:yellow");

    QLabel *ttsSpeedTitle = new QLabel(this);
    ttsSpeedTitle->setText(tr("TTS速度"));
    ttsSpeedTitle->setFont(QFont(ZITI, 20, QFont::Bold));
    ttsSpeedTitle->setGeometry(QRect(250, 122 - 30, 120, 45));
    ttsSpeedTitle->setStyleSheet("color:white;text-align:center");

    ttsSpeedText = new QLabel(this);
    ttsSpeedText->setText(QString("%1").arg(StationInfo.AutoVolume));
    ttsSpeedText->setAlignment(Qt::AlignCenter);
    ttsSpeedText->setFont(QFont(ZITI, 20, QFont::Normal));
    ttsSpeedText->setGeometry(QRect(420 - 10, 75 - 30 + 50, 60, 40));
    ttsSpeedText->setStyleSheet("color:black;text-align:center;background:yellow");

    QLabel *ttsTimbreTitle = new QLabel(this);
    ttsTimbreTitle->setText(tr("TTS音色"));
    ttsTimbreTitle->setFont(QFont(ZITI, 20, QFont::Bold));
    ttsTimbreTitle->setGeometry(QRect(250, 172 - 30, 120, 45));
    ttsTimbreTitle->setStyleSheet("color:white;text-align:center");

    ttsTimbreText = new QLabel(this);
    ttsTimbreText->setText(QString("%1").arg(StationInfo.AutoVolume));
    ttsTimbreText->setAlignment(Qt::AlignCenter);
    ttsTimbreText->setFont(QFont(ZITI, 20, QFont::Normal));
    ttsTimbreText->setGeometry(QRect(420 - 10, 75 - 30 + 100, 60, 40));
    ttsTimbreText->setStyleSheet("color:black;text-align:center;background:yellow");

    QGroupBox *ttsGroup = new QGroupBox(this);
    ttsGroup->setGeometry(210, 10, 340, 180);
    ttsGroup->setTitle("TTS");
    ttsGroup->setFont(QFont(ZITI, 20, QFont::Normal));

    QGroupBox *DCUGroup = new QGroupBox(this);
    DCUGroup->setGeometry(210, 190, 340, 180);
    DCUGroup->setTitle("呼叫台");
    DCUGroup->setFont(QFont(ZITI, 20, QFont::Normal));

    ManVolumeLB = new QLabel(this);
    ManVolumeLB->setText(tr("人工音量"));
    ManVolumeLB->setFont(QFont(ZITI, 20, QFont::Bold));
    ManVolumeLB->setGeometry(QRect(250, 122 + 100, 120, 45));
    ManVolumeLB->setStyleSheet("color:white;text-align:center");

    ManVolumeDecrease = new QPushButton(this);
    ManVolumeDecrease->setGeometry(QRect(370, 125 + 100, 40, 40));
    ManVolumeDecrease->setStyleSheet(
        "QPushButton{border-image: url(:/images/DecreasePulled.png)}"
        "QPushButton:pressed{border-image: url(:/images/DecreasePushed.png);}");
    connect(ManVolumeDecrease, SIGNAL(clicked()), this, SLOT(DecreaseManVolume()));

    ManVolumeIncrease = new QPushButton(this);
    ManVolumeIncrease->setGeometry(QRect(470, 125 + 100, 40, 40));
    ManVolumeIncrease->setStyleSheet(
        "QPushButton{border-image: url(:/images/IncreasePulled.png)}"
        "QPushButton:pressed{border-image: url(:/images/IncreasePushed.png);}");
    connect(ManVolumeIncrease, SIGNAL(clicked()), this, SLOT(IncreaseManVolume()));

    ManVolume = new QLabel(this);
    ManVolume->setText(QString("%1").arg(StationInfo.ManVolume));
    ManVolume->setAlignment(Qt::AlignCenter);
    ManVolume->setFont(QFont(ZITI, 20, QFont::Normal));
    ManVolume->setGeometry(QRect(420, 125 + 100, 40, 40));
    ManVolume->setStyleSheet("color:white;text-align:center;");

    GBJTVolumeLB = new QLabel(this);
    GBJTVolumeLB->setText(tr("监听音量"));
    GBJTVolumeLB->setFont(QFont(ZITI, 20, QFont::Bold));
    GBJTVolumeLB->setGeometry(QRect(250, 172 + 100, 120, 45));
    GBJTVolumeLB->setStyleSheet("color:white;text-align:center");

    GBJTVolumeDecrease = new QPushButton(this);
    GBJTVolumeDecrease->setGeometry(QRect(370, 175 + 100, 40, 40));
    GBJTVolumeDecrease->setStyleSheet(
        "QPushButton{border-image: url(:/images/DecreasePulled.png)}"
        "QPushButton:pressed{border-image: url(:/images/DecreasePushed.png);}");
    connect(GBJTVolumeDecrease, SIGNAL(clicked()), this, SLOT(DecreaseGBJTVolume()));

    GBJTVolumeIncrease = new QPushButton(this);
    GBJTVolumeIncrease->setGeometry(QRect(470, 175 + 100, 40, 40));
    GBJTVolumeIncrease->setStyleSheet(
        "QPushButton{border-image: url(:/images/IncreasePulled.png)}"
        "QPushButton:pressed{border-image: url(:/images/IncreasePushed.png);}");
    connect(GBJTVolumeIncrease, SIGNAL(clicked()), this, SLOT(IncreaseGBJTVolume()));

    GBJTVolume = new QLabel(this);
    GBJTVolume->setText(QString("%1").arg(StationInfo.GBJTVolume));

    GBJTVolume->setAlignment(Qt::AlignCenter);
    GBJTVolume->setFont(QFont(ZITI, 20, QFont::Normal));
    GBJTVolume->setGeometry(QRect(420, 175 + 100, 40, 40));
    GBJTVolume->setStyleSheet("color:white;text-align:center;");

    OK = new QPushButton(this);
    OK->setText(tr("确认"));
    OK->setFont(QFont(ZITI, 20, QFont::Normal));
    OK->setGeometry(QRect(393, 300 + 70, 111, 50));
    OK->setStyleSheet(
        "QPushButton{border-image: url(:/images/ButtonPulled.png);color:rgb(19,60,76)}"
        "QPushButton:pressed{border-image: url(:/images/ButtonPushed.png);}");
    connect(OK, SIGNAL(clicked()), this, SLOT(setVolume()));

    Cancel = new QPushButton(this);
    Cancel->setText(tr("返回"));
    Cancel->setFont(QFont(ZITI, 20, QFont::Normal));
    Cancel->setGeometry(QRect(515, 300 + 70, 111, 50));
    Cancel->setStyleSheet(
        "QPushButton{border-image: url(:/images/ButtonPulled.png);color:rgb(19,60,76)}"
        "QPushButton:pressed{border-image: url(:/images/ButtonPushed.png);}");

    mimajihuo = new QPushButton(this);
    mimajihuo->setText(tr("激活"));
    mimajihuo->setFont(QFont(ZITI, 18, QFont::Normal));
    mimajihuo->setGeometry(QRect(430, 77, 90, 90));
    mimajihuo->setStyleSheet(
        "QPushButton{border-image: url(:/images/ButtonPulled.png);color:rgb(19,60,76)}"
        "QPushButton:pressed{border-image: url(:/images/ButtonPushed.png);}");
    mimajihuo->hide();

    Volume.push_back(StationInfo.AutoVolume);
    Volume.push_back(StationInfo.MediaVolume);
    Volume.push_back(StationInfo.ManVolume);
    Volume.push_back(StationInfo.GBJTVolume);
    Volume.push_back(StationInfo.RadioVolume);
    Volume.push_back(StationInfo.DJJTVolume);
    Volume.push_back(StationInfo.DJVolume);
}

void VolumeSet::DecreaseAutoVolume()
{
    if (Volume[0] > 0)
    {
        Volume[0]--;
    }
    AutoVolume->setText(QString("%1").arg(Volume[0]));
}

void VolumeSet::IncreaseAutoVolume()
{
    if (Volume[0] < 9)
    {
        Volume[0]++;
    }
    AutoVolume->setText(QString("%1").arg(Volume[0]));
}

void VolumeSet::DecreaseManVolume()
{
    if (Volume[2] > 0)
    {
        Volume[2]--;
    }
    ManVolume->setText(QString("%1").arg(Volume[2]));
}

void VolumeSet::IncreaseManVolume()
{
    if (Volume[2] < 9)
    {
        Volume[2]++;
    }
    ManVolume->setText(QString("%1").arg(Volume[2]));
}

void VolumeSet::DecreaseGBJTVolume()
{
    if (Volume[3] > 0)
    {
        Volume[3]--;
    }
    GBJTVolume->setText(QString("%1").arg(Volume[3]));
}

void VolumeSet::IncreaseGBJTVolume()
{
    if (Volume[3] < 9)
    {
        Volume[3]++;
    }
    GBJTVolume->setText(QString("%1").arg(Volume[3]));
}

void VolumeSet::setVolume()
{
    if (StationInfo.ManVolume != Volume[2])
    {
        StationInfo.ManVolume = Volume[2];

        emit sendSetVolume(QString("设置人工音量：%1").arg(StationInfo.ManVolume));
    }

    if (StationInfo.GBJTVolume != Volume[3])
    {
        StationInfo.GBJTVolume = Volume[3];

        emit sendSetVolume(QString("设置广播监听音量：%1").arg(StationInfo.GBJTVolume));

        qDebug() << "StationInfo.GBJTVolume = " << StationInfo.GBJTVolume;
    }

    msgBoxShow("设置成功");
}

void VolumeSet::msgBoxShow(QString msg)
{
    if (!msgBox.isVisible())
    {
        msgBox.setText(msg);
        msgBox.setFont(QFont(ZITI, 20, QFont::Bold));
        msgBox.setStyleSheet("color:yellow;text-align:center;");
        msgBox.show();
    }
}

void VolumeSet::msgBoxClose()
{
    if (msgBox.isVisible())
    {
        msgBox.close();
    }
}

void VolumeSet::syncVolume(QList<int> volume)
{
    if (volume.size() == 5)
    {
        AutoVolume->setText(QString::number(volume.at(0)));
        ttsSpeedText->setText(QString::number(volume.at(1)));
        ttsTimbreText->setText(QString::number(volume.at(2)));
        ManVolume->setText(QString::number(volume.at(3)));
        GBJTVolume->setText(QString::number(volume.at(4)));
        Volume[2]              = volume.at(3);  //人工
        Volume[3]              = volume.at(4);  //监听
        StationInfo.AutoVolume = volume.at(0);
    }
}
